#include <arch.h>
#include <t8.h>
#include <util.h>
#include "i2c.h"

#define WRDATA      (1)
#define POLLACK     (2)
#define RDDATA      (3)
#define SETRDADDR   (4)

static U8 _iicData[32];
static volatile int _iicDataCount;
static volatile int _iicStatus;
static volatile int _iicMode;
static int _iicPt;


//**********************[IicPoll ]**************************************
void IicPoll(void)
{
    U32 iicSt,i;

    iicSt = rIICSTAT;
    if(iicSt & 0x8){}                   //When bus arbitration is failed.
    if(iicSt & 0x4){}                   //When a slave address is matched with IICADD
    if(iicSt & 0x2){}                   //When a slave address is 0000000b
    if(iicSt & 0x1){}                   //When ACK isn't received

    switch(_iicMode)
    {
        case POLLACK:
            _iicStatus = iicSt;
            break;

        case RDDATA:
            if((_iicDataCount--)==0)
            {
                _iicData[_iicPt++] = rIICDS;

                rIICSTAT = 0x90;                //Stop MasRx condition
                rIICCON  = 0xaf;                //Resumes IIC operation.
                ostdelay(1);                       //Wait until stop condtion is in effect.
                                                //Too long time...
                                                //The pending bit will not be set after issuing stop condition.
                break;
            }
            _iicData[_iicPt++] = rIICDS;
                        //The last data has to be read with no ack.
            if((_iicDataCount)==0)
                rIICCON = 0x2f;                 //Resumes IIC operation with NOACK.
            else
                rIICCON = 0xaf;                 //Resumes IIC operation with ACK
            break;

        case WRDATA:
            if((_iicDataCount--)==0)
            {
                rIICSTAT = 0xd0;                //stop MasTx condition
                rIICCON  = 0xaf;                //resumes IIC operation.
                ostdelay(1);                       //wait until stop condtion is in effect.
                       //The pending bit will not be set after issuing stop condition.
                break;
            }
            rIICDS = _iicData[_iicPt++];        //_iicData[0] has dummy.
            for(i=0;i<10;i++);                  //for setup time until rising edge of IICSCL
            rIICCON = 0xaf;                     //resumes IIC operation.
            break;

        case SETRDADDR:
//          printk("[S%d]",_iicDataCount);
            if((_iicDataCount--)==0)
            {
                break;                  //IIC operation is stopped because of IICCON[4]
            }
            rIICDS = _iicData[_iicPt++];
            for(i=0;i<10;i++);          //for setup time until rising edge of IICSCL
            rIICCON = 0xaf;             //resumes IIC operation.
            break;

        default:
            break;
    }
}

//**********************[ Run_IicPoll ]*********************************
void Run_IicPoll(void)
{
    if(rIICCON & 0x10)                  //Tx/Rx Interrupt Enable
       IicPoll();
}

//0:success
//other : wrong
int InitI2c(void)
{
    static int i2c_init = 0;

    if (i2c_init) return 0;

    rGPEUP  |= 0xc000;                  //Pull-up disable
    rGPECON |= 0xa0000000;                //GPE15:IICSDA , GPE14:IICSCL

      //Enable ACK, Prescaler IICCLK=PCLK/16, Enable interrupt, Transmit clock value Tx clock=IICCLK/16
    rIICCON  = (1<<7) | (0<<6) | (1<<5) | (0xf);

    rIICADD  = 0x10;                    //2442 slave address = [7:1]
    rIICSTAT = 0x10;                    //IIC bus data output enable(Rx/Tx)
    //rIICLC = (1<<2)|(3);  // Filter enable, 15 clocks SDA output ostdelay     added by junon

    i2c_init = 1;

    return 0;
}

//0:OK
//other: wrong
int I2cWriteByte(unsigned char slaveAddr, unsigned char reg, unsigned char val)
{
    _iicMode      = WRDATA;
    _iicPt        = 0;
    _iicData[0]   = (U8)reg;
    _iicData[1]   = val;
    _iicDataCount = 2;

    rIICDS        = slaveAddr;            //0xa0
      //Master Tx mode, Start(Write), IIC-bus data output enable
      //Bus arbitration sucessful, Address as slave status flag Cleared,
      //Address zero status flag cleared, Last received bit is 0
    rIICSTAT      = 0xf0;
      //Clearing the pending bit isn't needed because the pending bit has been cleared.
    while(_iicDataCount!=-1)
       Run_IicPoll();

    _iicMode = POLLACK;

    while(1)
    {
        rIICDS     = slaveAddr;
        _iicStatus = 0x100;             //To check if _iicStatus is changed
        rIICSTAT   = 0xf0;              //Master Tx, Start, Output Enable, Sucessful, Cleared, Cleared, 0
        rIICCON    = 0xaf;              //Resumes IIC operation.
        while(_iicStatus==0x100)
            Run_IicPoll();

        if(!(_iicStatus & 0x1))
            break;                      //When ACK is received
    }
    rIICSTAT = 0xd0;                    //Master Tx condition, Stop(Write), Output Enable
    rIICCON  = 0xaf;                    //Resumes IIC operation.
    ostdelay(1);                           //Wait until stop condtion is in effect.
      //Write is completed.

      return 0;
}

//0:success
//other:fail
int I2cReadByte(unsigned char slaveAddr, unsigned char reg, unsigned char * pval)
{
    _iicMode      = SETRDADDR;
    _iicPt        = 0;
    _iicData[0]   = (U8)reg;
    _iicDataCount = 1;

    rIICDS   = slaveAddr;
    rIICSTAT = 0xf0;                    //MasTx,Start
      //Clearing the pending bit isn't needed because the pending bit has been cleared.
    while(_iicDataCount!=-1)
        Run_IicPoll();

    _iicMode      = RDDATA;
    _iicPt        = 0;
    _iicDataCount = 1;

    rIICDS   = slaveAddr;
    rIICSTAT = 0xb0;                    //Master Rx,Start
    rIICCON  = 0xaf;                    //Resumes IIC operation.
    while(_iicDataCount!=-1)
        Run_IicPoll();

    *pval = _iicData[1];

    return 0;
}


//0:OK
//other: wrong
int I2cWriteBuffer(unsigned char slaveAddr, unsigned char *buf, int len)
{
    int i;

    _iicMode      = WRDATA;
    _iicPt        = 0;
    _iicDataCount = len;

    for (i = 0; i < len; i++) {
        _iicData[i]   = buf[i];
    }

    rIICDS        = slaveAddr;            //0xa0
      //Master Tx mode, Start(Write), IIC-bus data output enable
      //Bus arbitration sucessful, Address as slave status flag Cleared,
      //Address zero status flag cleared, Last received bit is 0
    rIICSTAT      = 0xf0;
      //Clearing the pending bit isn't needed because the pending bit has been cleared.
    while(_iicDataCount!=-1)
       Run_IicPoll();

    _iicMode = POLLACK;

    while(1)
    {
        rIICDS     = slaveAddr;
        _iicStatus = 0x100;             //To check if _iicStatus is changed
        rIICSTAT   = 0xf0;              //Master Tx, Start, Output Enable, Sucessful, Cleared, Cleared, 0
        rIICCON    = 0xaf;              //Resumes IIC operation.
        while(_iicStatus==0x100)
            Run_IicPoll();

        if(!(_iicStatus & 0x1))
            break;                      //When ACK is received
    }
    rIICSTAT = 0xd0;                    //Master Tx condition, Stop(Write), Output Enable
    rIICCON  = 0xaf;                    //Resumes IIC operation.
    ostdelay(1);                           //Wait until stop condtion is in effect.
    //Write is completed.

    return 0;
}

//0:success
//other:fail
int I2cReadWord(unsigned char slaveAddr, unsigned char reg, unsigned short * pval)
{
    _iicMode      = SETRDADDR;
    _iicPt        = 0;
    _iicData[0]   = (U8)reg;
    _iicDataCount = 1;

    rIICDS   = slaveAddr;
    rIICSTAT = 0xf0;                    //MasTx,Start
      //Clearing the pending bit isn't needed because the pending bit has been cleared.
    while(_iicDataCount!=-1)
        Run_IicPoll();

    _iicMode      = RDDATA;
    _iicPt        = 0;
    _iicDataCount = 2;

    rIICDS   = slaveAddr;
    rIICSTAT = 0xb0;                    //Master Rx,Start
    rIICCON  = 0xaf;                    //Resumes IIC operation.
    while(_iicDataCount!=-1)
        Run_IicPoll();

    *pval = (_iicData[1] << 8) | _iicData[2];

    return 0;
}



